package cn.elwin.samples;

import java.util.ArrayList;
import java.util.HashMap;

import android.media.Image;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class TwoListView extends Activity {

	private String[] str = { "a", "b", "c", "d", "e", "f", "g" };
	private ArrayList<HashMap<String, Object>> item_list = new ArrayList<HashMap<String, Object>>();
	private ListView list_view_up;
	private ListView list_view_down;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two_list_view);

		for (int i = 0; i < 10; ++i) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("item_image", R.drawable.ic_launcher);
			map.put("item_title", "标题" + i);
			map.put("item_text", "第" + i + "行的内容");
			item_list.add(map);
		}

		list_view_up = (ListView) findViewById(R.id.list_view_up);
		list_view_down = (ListView) findViewById(R.id.list_view_down);

		list_view_up.setAdapter(new SimpleAdapter(this, item_list,
				R.layout.layout_list_view_custom, new String[] { "item_image",
						"item_title", "item_text" }, new int[] {
						R.id.item_image, R.id.item_title, R.id.item_text }));
		list_view_down.setAdapter(new MyAdapter(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.two_column_list_view, menu);
		return true;
	}

	/** 添加一个得到数据的方法，方便使用 */

	private ArrayList<HashMap<String, Object>> getDate() {

		ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

		/** 为动态数组添加数据 */
		for (int i = 0; i < 30; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemTitle", "第" + i + "行");
			map.put("ItemText", "这是第" + i + "行");
//			map.put("ItemImage", R.drawable.ic_launcher);
			listItem.add(map);
		}
		return listItem;
	}

	public final class ViewHolder {
		public TextView title;
		public TextView text;
		public ImageView image;
	}

	private class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;// 得到一个LayoutInfalter对象用来导入布局

		/** 构造函数 */
		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return getDate().size();// 返回数组的长度
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder holder;

			Log.v("MyListViewBase", "getView " + position + " " + convertView); // 观察convertView随ListView滚动情况
			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.layout_list_view_custom, null);
				holder = new ViewHolder();
				/** 得到各个控件的对象 */
				holder.title = (TextView) convertView
						.findViewById(R.id.item_title);
				holder.text = (TextView) convertView
						.findViewById(R.id.item_text);
				holder.image = (ImageView) convertView
						.findViewById(R.id.item_image);
				convertView.setTag(holder);// 绑定ViewHolder对象
			} else {
				holder = (ViewHolder) convertView.getTag();// 取出ViewHolder对象
			}
			/** 设置TextView显示的内容，即我们存放在动态数组中的数据 */
			holder.title.setText(getDate().get(position).get("ItemTitle")
					.toString());
			holder.text.setText(getDate().get(position).get("ItemText")
					.toString());
			holder.image.setImageResource(R.drawable.ic_launcher);

			return convertView;

		}
	}
}
